package com.behome.netty.netty_A.bean.A50Bean;

import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.netty_A.bean.ProtocolAPacket;
import com.behome.netty.netty_A.enums.ProtocolA50Annotation;
import com.behome.netty.netty_A.enums.ProtocolA50Type;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;


@ProtocolA50Annotation(
        type = ProtocolA50Type.HEARTBEAT
)
@Slf4j
public class AdapterHeartBeat extends ProtocolAPacket {
    private static final int CMD_BODY_MIN_LENGTH = 6;
    private String frontCode;
    private byte frontState;
    private Date connectionTime;

    public AdapterHeartBeat() {
        super(null, REBACK_TYPE.主动上报);
    }

    public String getFrontCode() {
        return this.frontCode;
    }

    public AdapterHeartBeat setFrontCode(String frontCode) {
        this.frontCode = frontCode;
        return this;
    }

    public byte getFrontStateNew() {

        return this.frontState;
    }

    public String getFrontState() {
        String state = "";
        switch (this.frontState) {
            case 1:
                state = "空闲";
                break;
            case 2:
                state = "工作";
                break;
            case 3:
                state = "故障";
                break;
            default:
                state = "未知";
        }

        return state;
    }

    public AdapterHeartBeat setFrontState(byte frontState) {
        if (frontState != 1 && frontState != 2 && frontState != 3) {
            log.error("frontState只能从1,2,3中取值");
        }

        this.frontState = frontState;
        return this;
    }

    public Date getConnectionTime() {
        return this.connectionTime;
    }

    public AdapterHeartBeat setConnectionTime(Date connectionTime) {
        this.connectionTime = connectionTime;
        return this;
    }

    protected byte[] serializeBody() {
        byte[] ret = null;
        if (this.validateBeforeSerialize()) {
            byte[] fronCodeBytes = ByteConventerKit.serializeResourceCode(this.frontCode);
            byte frontCodeLength = (byte) fronCodeBytes.length;
            byte[] timeBytes = ByteConventerKit.datetimeToBytes(this.connectionTime);
            int bodyLength = 6 + fronCodeBytes.length;
            ByteBuffer bodyBuffer = ByteBuffer.allocate(bodyLength);
            bodyBuffer.put(frontCodeLength).put(fronCodeBytes).put(this.frontState).put(timeBytes);
            ret = bodyBuffer.array();
        }

        return ret;
    }

    protected void deserializeBody(byte[] bodyData) {
        if (bodyData != null && bodyData.length > 0) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(bodyData);
            byte frontCodeLength = byteBuffer.get();
            byte[] tmp = new byte[frontCodeLength];
            byteBuffer.get(tmp);
            this.frontCode = ByteConventerKit.deserializeResourceCode(tmp);
            this.frontState = byteBuffer.get();
            byte[] timeBytes = new byte[4];
            byteBuffer.get(timeBytes);
            this.connectionTime = ByteConventerKit.bytesToDatetime(timeBytes);
        }

    }

    public String toDetailString() {
        return this.getCmdTypeName() + " [frontCode=" + this.frontCode + ", frontState=" + this.getFrontState() + ", connectionTime=" + this.connectionTime + "]";
    }

    public String toJsonString() {
        String state = "";
        if (this.frontState == 1) {
            state = "空闲";
        } else if (this.frontState == 2) {
            state = "工作";
        } else if (this.frontState == 3) {
            state = "故障";
        }

        return "{\"资源编码\":\"" + this.frontCode + "\",\"工作状态\":\"" + state + "\",\"连接时间\":\"" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(this.connectionTime) + "\"}";
    }

    public boolean validateBeforeSerialize() {
        boolean ret = true;
        if (StringUtil.isNullOrEmpty(this.frontCode)) {
            log.error("frontCode不能为空");
            ret = false;
        }

        if (this.frontState != 1 && this.frontState != 2 && this.frontState != 3) {
            log.error("frontState取值非法");
            ret = false;
        }

        return ret;
    }

    public String toCHJsonString() {
        String state = "";
        if (this.frontState == 1) {
            state = "空闲";
        } else if (this.frontState == 2) {
            state = "工作";
        } else if (this.frontState == 3) {
            state = "故障";
        }
        return "{\"资源编码\":\"" + this.frontCode + "\",\"工作状态\":\"" + state + "\",\"连接时间\":\"" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(this.connectionTime) + "\"}";
    }

    public String getSourceCode() {
        return this.frontCode;
    }
}
